define(function (require) {
    "use strict";
    var ko = require("knockout");
    var router = require("plugins/router");
    var layui = require("layui");
    var $ = require("jquery");
    var system = require("durandal/system");
    var isFinishTableTemp = require("text!./isFinish-table-temp.html");
    var isRunningTableTemp = require("text!./isRunning-table-temp.html");
    var viewBar = require("text!./view-bar.html");

    var param = {
        planId: ko.observable("")
    };

    var tableModel = {
        id: "account-list",
        elem: "#list-table", //指定原始表格元素选择器（推荐id选择器）
        height: "full-227", //容器高度
        cols: [[
            //标题栏
            {field: "id", title: "ID", width: 70, fixed: "left"},
            {title: "操作", width: 310, align: "center", toolbar: viewBar},
            {field: "urlsLeft", title: "等待中的 url 数量", width: 138},
            {field: "isFinish", title: "是否已完成", width: 100, templet: isFinishTableTemp},
            {field: "isRunning", title: "是否运行中", width: 100, templet: isRunningTableTemp},
            {field: "lastestRunTime", title: "最近启动时间", width: 194},
            {field: "finishedTime", title: "完成时间", width: 194}
        ]], //设置表头
        url: "/back/actions",
        response: {
            statusCode: 200
        }
    };

    var tableIns = null;
    var modelId = null;

    return {
        //router: router,
        param: param,
        activate: function (id) {
            modelId = id;
            param.planId(modelId);
        },
        compositionComplete: function () {
            layui.use(["laytpl", "table", "element", "layer", "laydate", "form"], function () {
                var laytpl = layui.laytpl;
                var table = layui.table;
                var element = layui.element;
                var layer = layui.layer;
                var laydate = layui.laydate;
                var form = layui.form;

                tableModel.where = {
                    planId: param.planId()
                };

                tableIns = table.render(tableModel);

                table.on("tool(list-table)", function (obj) {
                    var data = obj.data;
                    if (obj.event === "review") {
                        router.navigate("plan/review/param/" + modelId + "?actionId=" + data.id);
                    } else if (obj.event === "trace") {
                        router.navigate("plan/trace/" + modelId + "?actionId=" + data.id);
                    } else if (obj.event === "keyword") {
                        router.navigate("plan/keyword/" + modelId + "?actionId=" + data.id);
                    } else if (obj.event === "sourceKeyword") {
                        router.navigate("plan/sourceKeyword/" + modelId + "?actionId=" + data.id);
                    }
                });

                form.on("submit(search-submit)", function (data) {
                    tableIns.reload({
                        where: {
                            planId: param.planId()
                        }
                    });
                    return false;
                });
            });
        }
    };
});